package com.body;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.connect.connect;
import com.root.root;

public class login extends HttpServlet{
	
	private root root;
	private String loginname;       
	private String loginpass;       
	private String remember;        
	private connect conn;
	private String tousernumber;
	private String from;
	private Connection con ;
	private Statement stat;
	private ResultSet rs;           
	private HttpSession session;  // session
	private Cookie ck;            // cookie
	private PrintWriter out;     
	private String mutex = "";          
	private int flag=0;
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	    synchronized (mutex){
	    	root = new root();
			conn = new connect();
			con = conn.con();
			out = resp.getWriter();
			session = req.getSession(); 
			
			loginname = req.getParameter("loginname");
			loginpass = req.getParameter("loginpwd");
			remember = req.getParameter("remember");
			from = req.getParameter("from");
            if(from==null)
            	from = "0";
			tousernumber = (String)session.getAttribute("tousernumber");
			
			resp.setHeader("Content-type", "text/html;charset=utf-8"); 
			resp.setCharacterEncoding("utf-8");

			if(loginname.equals(root.getNumber()) && loginpass.equals(root.getPassword())){
				session.setAttribute("IsRoot", "yes");
				session.setAttribute("username", root.getName());
				flag = 1;
			} else {
				try{
					stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
					rs = stat.executeQuery("SELECT * FROM user WHERE UserNumber='"+loginname+"' AND UserPassword = '"+loginpass+"'");

					if(rs.next()){					
						flag = 1;
						session.setAttribute("username", loginname);
					}
					else{
						flag = 0;
					}
					con.close();	
				}catch(SQLException e){
					e.printStackTrace();
				}	
			}
			if(flag == 1){
				session.setAttribute("yes", "yes");
                session.setAttribute("password", loginpass);
                
                //remember username  set cookie
                if("on".equals(remember)){
                	ck = new Cookie("name",loginname);
                	ck.setMaxAge(3600*24*15);
                	resp.addCookie(ck);
                	ck = new Cookie("password",loginpass);
                	ck.setMaxAge(3600*24*15);
                	resp.addCookie(ck);     
                }
                else{  // delete cookie
                	ck = new Cookie("name",loginname);
                	ck.setMaxAge(0);   
                	resp.addCookie(ck);
                	ck = new Cookie("password",loginpass);
                	ck.setMaxAge(0);
                	resp.addCookie(ck);     
                }
                if(from.equals("0")){
				    out.print("<script  type='text/javascript'  >; window.location.href='person.jsp';  </script>");
                } else if(from.equals("1")){
                	out.print("<script  type='text/javascript'  >; window.location.href='message.jsp?findmessage=';  </script>");
                } else if(from.equals("2")){
                	out.print("<script  type='text/javascript'  >; window.location.href='takemessage.jsp?tousername="+tousernumber+"';  </script>");
                }
			} else {
				out.print("<script>alert('ERROR Incorrect username or password');history.go(-1);</script>");
			}
			
		}
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
}

